﻿using System;
using System.ComponentModel.Composition;
using BizControl.Presentation.Infrastructure.Controls;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Modularity;
using BizControl.Presentation.Infrastructure.Constants;
using System.Windows;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Input;
using Csla.Xaml;
using System.Windows.Controls;
using BizControl.Library.BusinessObjects.Marks;

namespace BizControl.Presentation.PersonMarkEditCompositeModule.ViewModels
{
  [Export]
  public class PersonMarkEditCompositeViewModel : DependencyObject, INavigationAware, IRegionMemberLifetime
  {
    private readonly DelegateCommand _okCommand;

    [Import(AllowRecomposition = false)]
    private IModuleManager _moduleManager;

    [Import(AllowRecomposition = false)]
    private IRegionManager _regionManager;

    public PersonMarkEditCompositeViewModel()
    {
      _okCommand = new DelegateCommand(OkCommandHandler);
    }

    #region Handlers

    private void OkCommandHandler()
    {
      _regionManager.RequestNavigate(ShellRegionNames.MainContentRegion, ViewNames.PersonMarksViewName, NavigationCompleted);
    }

    private void NavigationCompleted(NavigationResult result)
    {
      // confirmation
    }

    #endregion

    #region Properties

    public object ViewRegionContext
    {
      get { return _regionManager.Regions[RegionNames.PersonMarkEditMainRegion].Context; }
      set { _regionManager.Regions[RegionNames.PersonMarkEditMainRegion].Context = value; }
    }

    public ICommand OkCommand
    {
      get { return _okCommand; }
    }

    #endregion

    #region INavigationAware Members

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
      if (navigationContext.Uri.OriginalString.Contains(ViewNames.PersonMarkEditCompositeViewName))
        return true;
      return false;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
    }

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
      string markId = navigationContext.Parameters[NavigationParametersNames.IdName];
      string newPersonMark = navigationContext.Parameters[NavigationParametersNames.NewPersonMarkName];
      if (!string.IsNullOrEmpty(markId))
      {
        Guid id = Guid.Parse(markId);
        ViewRegionContext = PersonMarkBO.GetPersonMark(id);
      }
      else if (!string.IsNullOrEmpty(newPersonMark))
      {
        ViewRegionContext = PersonMarkBO.NewPersonMark();
      }
    }

    #endregion

    #region IRegionMemberLifetime Members

    public bool KeepAlive
    {
      get { return true; }
    }

    #endregion

    #region IConfirmNavigationRequest Members

    public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
    {
      // TO DO: confirmation window
    }

    #endregion
    
  }
}
